package cn.iocoder.yudao.module.agri.service.farmrecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.agri.controller.admin.farmrecord.vo.*;
import cn.iocoder.yudao.module.agri.dal.dataobject.farmrecord.FarmRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.agri.convert.farmrecord.FarmRecordConvert;
import cn.iocoder.yudao.module.agri.dal.mysql.farmrecord.FarmRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.agri.enums.ErrorCodeConstants.*;

/**
 * 农事记录 Service 实现类
 *
 * @author 广东小蜂
 */
@Service
@Validated
public class FarmRecordServiceImpl implements FarmRecordService {

    @Resource
    private FarmRecordMapper farmRecordMapper;

    @Override
    public Long createFarmRecord(FarmRecordCreateReqVO createReqVO) {
        // 插入
        FarmRecordDO farmRecord = FarmRecordConvert.INSTANCE.convert(createReqVO);
        farmRecordMapper.insert(farmRecord);
        // 返回
        return farmRecord.getId();
    }

    @Override
    public void updateFarmRecord(FarmRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateFarmRecordExists(updateReqVO.getId());
        // 更新
        FarmRecordDO updateObj = FarmRecordConvert.INSTANCE.convert(updateReqVO);
        farmRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteFarmRecord(Long id) {
        // 校验存在
        validateFarmRecordExists(id);
        // 删除
        farmRecordMapper.deleteById(id);
    }

    private void validateFarmRecordExists(Long id) {
        if (farmRecordMapper.selectById(id) == null) {
            throw exception(FARM_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public FarmRecordDO getFarmRecord(Long id) {
        return farmRecordMapper.selectById(id);
    }

    @Override
    public List<FarmRecordDO> getFarmRecordList(Collection<Long> ids) {
        return farmRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FarmRecordDO> getFarmRecordPage(FarmRecordPageReqVO pageReqVO) {
        return farmRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FarmRecordDO> getFarmRecordList(FarmRecordExportReqVO exportReqVO) {
        return farmRecordMapper.selectList(exportReqVO);
    }

}
